package com.indusfo.spc.client.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.indusfo.spc.client.mapper.ClientBatchMapper;
import com.indusfo.spc.client.mapper.ClientOrderChangeMapper;
import com.indusfo.spc.client.mapper.ClientOrderFlowMapper;
import com.indusfo.spc.client.pojo.ClientOrderChange;
import com.indusfo.spc.client.service.ClientOrderChangeService;
import com.indusfo.spc.exception.BusinessException;
import com.indusfo.spc.exception.ModifyFailedException;
import com.indusfo.spc.exception.ParamsErrorException;
import com.indusfo.spc.mapper.OrderTechMapper;
import com.indusfo.spc.pojo.Batch;
import com.indusfo.spc.pojo.OrderFlow;
import com.indusfo.spc.pojo.OrderTech;

/**
 * 客户端型号变更
 * 
 * @author zhengd
 *
 */
@Service
public class ClientOrderChangeServiceImpl implements ClientOrderChangeService {

	@Autowired
	private ClientOrderChangeMapper clientOrderChangeMapper;
	@Autowired
	private ClientBatchMapper clientBatchMapper;
	@Autowired
	private ClientOrderFlowMapper clientOrderFlowMapper;
	@Autowired
	private OrderTechMapper orderTechMapper;
	@Autowired
	private ClientAnoServiceImpl clientAnoServiceImpl;

	/**
	 * 保存变更
	 */
	@Override
	@Transactional
	public Integer saveClientOrderChange(ClientOrderChange clientOrderChange) {
		// 判断入参是否正确
		if (null == clientOrderChange.getBatchId()) {
			throw new ParamsErrorException("请传入批次ID！");
		}
		if (null == clientOrderChange.getOrderTechId()) {
			throw new ParamsErrorException("请传入任务单版本ID！");
		}
		if (null == clientOrderChange.getOrderId()) {
			throw new ParamsErrorException("请传入任务单ID！");
		}
		if (null == clientOrderChange.getNewOrderId()) {
			throw new ParamsErrorException("请传入新任务单ID！");
		}
		if (null == clientOrderChange.getNewOrderTechId()) {
			throw new ParamsErrorException("请传入新任务单版本ID！");
		}
		if (clientOrderChange.getOrderId() == clientOrderChange.getNewOrderId()) {
			throw new ParamsErrorException("相同任务单不能变更！");
		}
		// 保存变更信息
		Integer save = clientOrderChangeMapper.insertClientOrderChange(clientOrderChange);
		if (save != 1) {
			throw new BusinessException("保存失败！");
		}
		// 查询原先流程中已完成的最后一道流程
		OrderFlow orderFlow = clientOrderFlowMapper.queryLastOrderFlowByBatchId(clientOrderChange.getBatchId());
		if (null == orderFlow) {
			throw new BusinessException("未查到变更前流程的最后完成工序，无法变更！");
		}
		// 根据新任务单生成新版本
		OrderTech orderTech = new OrderTech();
		orderTech.setOrderId(clientOrderChange.getNewOrderId());
		orderTech.setUserId(clientOrderChange.getUserId());
		orderTech.setSourceTypeId(2);
		orderTech.setDataState(1);
		Integer count = orderTechMapper.insertOrderTech(orderTech);
		if (count == 0) {
			throw new ModifyFailedException("新增失败！");
		}
		Integer orderTechId = orderTech.getOrderTechId();
		// 声明新流程集合
		List<OrderFlow> newOrderFlows = new ArrayList<OrderFlow>();
		// 查询已完成工序集合并放入新流程集合中
		List<OrderFlow> finishOrderFlows = clientOrderFlowMapper.queryFinishProList(clientOrderChange.getOrderTechId(),
				orderFlow.getSerialNum());
		newOrderFlows.addAll(finishOrderFlows);
		// 查询新流程当前工序的序号
		Integer serialNum = clientOrderFlowMapper.querySerialNumByProId(orderFlow.getProId(),
				clientOrderChange.getNewOrderTechId());
		if (serialNum == null) {
			throw new BusinessException("未查到新任务单有当前已完成工序，无法变更！");
		}
		// 查询新任务单剩下工序集
		List<OrderFlow> lastOrderFlows = clientOrderFlowMapper.queryProList(clientOrderChange.getNewOrderTechId(),
				serialNum);
		// 合并放入新流程集合中
		newOrderFlows.addAll(lastOrderFlows);
		Integer newSerialNum = 1;
		// 更改新顺序号
		for (OrderFlow newOrderFlow : newOrderFlows) {
			newOrderFlow.setSerialNum(newSerialNum);
			newSerialNum++;
		}
		// 调用工检上报不良的复制流程到原工艺流程已完工工序下面
		clientAnoServiceImpl.copyOrderFlows(newOrderFlows, orderTechId, clientOrderChange.getOrderTechId());
		// 查询新的下道流程ID
		Integer newOrderFlowId = clientOrderFlowMapper.selectOrderFlowIdByOrderTechId(orderTechId,
				finishOrderFlows.size() + 1);
		Batch batch = clientBatchMapper.selectByBatchId(clientOrderChange.getBatchId());
		batch.setOrderId(clientOrderChange.getNewOrderId());
		batch.setOrderTechId(orderTechId);
		batch.setOrderFlowId(newOrderFlowId);
		batch.setOrderFlowStateId(1);
		// 修改批号与任务单的关联信息
		Integer update = clientBatchMapper.updateBatch(batch);
		if (update != 1) {
			throw new BusinessException("修改批号与任务单关联信息失败！");
		}
		return 1;
	}

	/**
	 * 根据批号查询基本信息
	 */
	@Override
	public ClientOrderChange selectClientOrderChangeByBatchCode(String batchCode) {
		return clientOrderChangeMapper.selectClientOrderChangeByBatchCode(batchCode);
	}

}
